/* 
 * File:   CommandParser.cpp
 * Author: Bram
 * 
 * Created on January 11, 2013, 12:08 AM
 */

#include "CommandParser.h"




CommandParser::CommandParser(const string & currSEND) {
    currentSender = currSEND;
}

CommandParser::CommandParser(const CommandParser& orig) {
}

CommandParser::~CommandParser() {
}

const string& CommandParser::getSender()
{
    return currentSender;
}

void CommandParser::getReceiver(string& dasReceiver)
{
    if(getSender().compare("CLIENT") == 0)
    {
        dasReceiver = "SERVER";
    } else {
        dasReceiver = "SERVER";
    }
}

//parsed een command wat binnekomt via de socket, deze moet dus van xml omgezet worden naar een command
Command * CommandParser::ParseInputCommand(const string & strFullCommand)
{
        Command* inputCmd = Command::parseInputCommand(strFullCommand);
    if(inputCmd == 0){
        inputCmd = ErrorCommand::initializeWithErrorMessage("ERROR - 3001 - ERROR PARSING INCOMMING SOCKET COMMAND!");
    }
    return inputCmd;
}

//command wat jij zelf ingetypt hebt wordt geparsed, deze moet dus nog verzonden worden
Command * CommandParser::ParseOutputCommand(const string & strFullCommand)
{
    Command* outputCmd = Command::ParseOutputCommand(strFullCommand);
    if(outputCmd == 0){
        outputCmd = ErrorCommand::initializeWithErrorMessage("ERROR - 3004 - ERROR PARSING USER INPUT COMMAND!");
    }
    return outputCmd;
}

//handelt het commando af, kijkt of we nu op de client of server zitten en handelt zo het commando verder af
void CommandParser::ExcecuteReveiveCommand(TcpSocket* clientORserver, Command * cmd, queue<Command *>& commandQueue)
{
    if(getSender().compare("CLIENT") == 0){
        cmd->setSender(getSender());
        cmd->ExcecuteClientReceiveCommand(clientORserver, commandQueue);
    } else if(getSender().compare("SERVER") == 0){
        cmd->setSender(getSender());
        cmd->ExcecuteServerReceiveCommand(clientORserver, commandQueue);
    } else {
        cout << "ERROR in CommandParser.cpp - ExcecuteReveiveCommand(), Command sender: '" << getSender() << "' is noet valid!! - should not ever happen! 88884" << endl;
        dasLog << "ERROR in CommandParser.cpp - ExcecuteReveiveCommand(), Command sender: '" << getSender() << "' is noet valid!! - should not ever happen! 88884" << endl;
    }
}

void CommandParser::ExcecuteSendCommand(TcpSocket* clientORserver, Command * cmd, queue<Command *>& commandQueue)
{
    if(getSender().compare("CLIENT") == 0){
        cmd->setSender(getSender());
        cmd->ExcecuteClientSendCommand(clientORserver, commandQueue);
    } else if(getSender().compare("SERVER") == 0){
        cmd->setSender(getSender());
        cmd->ExcecuteServerSendCommand(clientORserver, commandQueue);
    } else {
        cout << "ERROR in CommandParser.cpp - ExcecuteSendCommand(), Command sender: '" << getSender() << "' is noet valid!! - should not ever happen! 88885" << endl;
        dasLog << "ERROR in CommandParser.cpp - ExcecuteSendCommand(), Command sender: '" << getSender() << "' is noet valid!! - should not ever happen! 88885" << endl;
    }
}

void CommandParser::ReceiveFile(TcpSocket* client, Command * cmd)
{
    
}

//in de 1e parameter van de cmd zit het full file path!
void CommandParser::SendFile(TcpSocket* clientORserver, Command * cmd)
{
    
}